package net.baiken.yuki;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.text.AttributeSet;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;

import net.baiken.commons.Messages;
import net.baiken.messages.YukiMessageKeys;

public class AboutFrame extends JFrame implements HyperlinkListener {

	private JEditorPane aboutMessage;

	@Override
	public void paintComponents(Graphics g) {
		System.out.println("AboutFrame.paintComponents()");
	}

	public AboutFrame() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(400, 400);
		setTitle(Messages.getInstance().getString(YukiMessageKeys.TitleKey));
		setUndecorated(true);
		getRootPane().setBorder(BorderFactory.createEtchedBorder());
		getRootPane().remove(0);
		getRootPane().setBackground(Color.white);

		super.setLocationRelativeTo(null);
		Container contentPane = getRootPane();
		try {
			aboutMessage = new JEditorPane(ClassLoader.getSystemResource(String
					.format("net/baiken/messages/about_%s.html", Locale
							.getDefault())));
			aboutMessage.setBackground(Color.white);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		aboutMessage.setEditable(false);
		aboutMessage.addHyperlinkListener(this);
		aboutMessage.getDocument().putProperty("test", "hola");
		contentPane.setLayout(new BorderLayout());
		contentPane.add(aboutMessage, BorderLayout.CENTER);
	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == EventType.ACTIVATED) {
			Enum<YukiCommand> command = YukiCommand.valueOf(getCommand(e
					.getURL()));

			if (command == YukiCommand.None) {

				String target = (String) ((AttributeSet) e.getSourceElement()
						.getAttributes().getAttribute(Tag.A))
						.getAttribute(Attribute.TARGET);
				if (target == null) {

				} else if (target.equalsIgnoreCase("_blank")) {
					System.out.println("AboutFrame.hyperlinkUpdate():::::");
					try {
						Desktop.getDesktop().browse(
								new URI((String) ((AttributeSet) e
										.getSourceElement().getAttributes()
										.getAttribute(Tag.A))
										.getAttribute(Attribute.HREF)));
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						e1.printStackTrace();
					}
				}
			} else if (command == YukiCommand.HideAbout) {
				setVisible(false);
			}
		} else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
			aboutMessage.setCursor(Cursor.getDefaultCursor());
		} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
			aboutMessage.setCursor(Cursor
					.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	}

	private String getCommand(URL url) {
		String query = url.getQuery();
		if (query != null) {
			String[] keys = query.split("&");
			for (String entry : keys) {
				if (entry.startsWith("command")) {
					try {
						return URLDecoder.decode(entry.substring(entry
								.indexOf("=") + 1), "UTF-8");
					} catch (UnsupportedEncodingException e) {
					}
				}
			}
		}
		return "None";
	}

	public static void main(String[] args) {
		Messages.getInstance().setBaseName(
				"net.baiken.messages.MessageResource");
		AboutFrame frame = new AboutFrame();
		frame.setVisible(true);
	}
}
